<?php
/**
 * Created by PhpStorm.
 * User: qishengqiang
 * Date: 2022-06-04
 * Time: 21:24
 */

namespace app\services;


use app\models\Organizations;
use app\models\Provinces;

class ChartService extends ServiceBase
{
    public function getCharts($appletId)
    {
        if ($appletId == 1) {
            $arr[] = [
                'chartId' => 1,
                'chartName' => '高校',
                'chartCategories' => $this->getChartCategories($appletId, 1)
            ];
        } elseif ($appletId == 2) {
            $arr[] = [
                'chartId' => 2,
                'chartName' => '社会组织',
                'chartCategories' => $this->getChartCategories($appletId, 2)
            ];

//            $arr[] = [
//                'chartId' => 1,
//                'chartName' => '高校',
//                'chartCategories' => $this->getChartCategories($appletId, 1)
//            ];

        }

        return $arr;
    }

    public function getChartCategories($applet, $type = 1)
    {
        $return = [];

        $provinceCodes = Organizations::where(['appletId' => $applet, 'status' => 1, 'categoryId' => $type])->group('provinceCode')->field('provinceCode')->select();

        if (count($provinceCodes)) {
            foreach ($provinceCodes as $provinceCode) {
                $codes[] = $provinceCode->provinceCode;
            }

            $provinces = Provinces::where('provinceCode', 'in', $codes)->order(['provinceId' => 'asc'])->select();

            foreach ($provinces as $province) {
                $return[] = [
                    'provinceId' => $province->provinceId,
                    'provinceCode' => $province->provinceCode,
                    'provinceName' => $province->provinceName,
                ];
            }
        }

        return $return;
    }
}